<?php
/**
 * Created by PhpStorm.
 * User: kcross
 * Date: 20-6-26
 * Time: 下午3:57
 */
namespace App\Lib\Sdk\Spider;

use App\Lib\Sdk\Spider\Service\Spider;
use App\Models\User;
use Illuminate\Support\Facades\Log;

class Jd2Spider extends BaseSpider implements Spider
{

    protected $searchAble = [
        "so", "rank", "page", "num", "type","appid","appkey"
    ];

    /**
     * @param array $jdConfig
     * @param null $itemId
     * @return mixed
     * @throws \Exception
     */
    public function search(array $jdConfig, $itemId = null)
    {
        /*$config = User::getConfig();
        if (empty($config['jd_pid'])) {
            throw new \Exception("该用户未设置京东PID,请联系系统管理员!!");
        }*/
        $api = JdApi::list;
        $params = array_merge($jdConfig, [
            'appid'=> $this->config['appid'],
            "appkey"=> $this->config['appkey'],
            'num' => 50
        ]);
        $filter = [];
        foreach ($this->searchAble as $key) {
            if (isset($params[$key])) {
                $filter[$key] = $params[$key];
            }
        }
        $res = $this->client->post($api, $filter);
        $data = $res->result->toArray();
        foreach ($data as &$item) {
            $item['shortURL'] = "";
          //  $item['shortURL'] = $this->getLink($item);
        }
        return $data;

    }


   public function search2($itemId)
    {
        $config = User::getConfig();
        if (empty($config['jd_pid'])) {
            throw new \Exception("该用户未设置京东PID,请联系系统管理员!!");
        }
        $api = JdApi::list;
        $params =[
            'appid'=> $this->config['appid'],
            "appkey"=> $this->config['appkey'],
            'so' => $itemId
        ];
        $res = $this->client->post($api, $params);

        return $res->toArray();

    }

    public function getLink($item, $uid, $ext = [])
    {

        $link='http://api.josapi.net/goodsquery?skuIds='.$item['goods_id'];
        $resr =file_get_contents($link);
        $resrs=\GuzzleHttp\json_decode($resr, true);
        $couponUrl='';
        if($resrs['error']==0){
            if(!empty($resrs['data'][0]['couponInfo']['couponList'])){
                $couponUrl=urlencode($resrs['data'][0]['couponInfo']['couponList'][0]['link']);
            }
        }
        $url='http://api.josapi.net/prombyuid?unionId='.$this->config['unionid'].'&materialId=item.jd.com/'.$item['goods_id'].'.html&positionId='.$ext['uid'].'&couponUrl='.$couponUrl;
        $res =file_get_contents($url);
        //print_r($res);exit;
        $re=\GuzzleHttp\json_decode($res, true);
        //print_r($re);exit;
        if($re['error']==0){
            //Log::error($re['data']['shortURL']);
            return $re['data']['shortURL'];
        }else{
            return '';
        }
    }

    public function getLink_bak($item, $uid, $ext = [])
    {
        $api = JdApi::prom;
        $config = User::getConfig($uid);
        $params = [
            'appid'=> $this->config['appid'],
            "appkey"=> $this->config['appkey'],
            'unionid' => $this->config['unionid'],
            'gid' => $item['goods_id'],
            'positionid' => $config['jd_pid'],
            'coupon_url' => $item['discount_link']
        ];
        $res = $this->client->post($api, $params);
        if (isset($res->result) && isset($res->result->link)) {
            return $res->result->link;
        }
        return "";
    }


    /**
     * @param $uid
     * @return mixed
     * {
    "return": "0",
    "result": {
    "unionId": "1000369430",
    "name": "创建A4",
    "positionid": 1585637524
    }
    }
     */
    public function createPid($uid)
    {
        return "";
        $api = JdApi::createPid;
        $params = [
            'appid'=> $this->config['appid'],
            "appkey"=> $this->config['appkey'],
            'unionid' => $this->config['unionid'],
            'key' => $this->config['key'],
            'name' => "user_" . $uid . "的推荐位",
        ];
        $res = $this->client->post($api, $params);
        return $res->result->positionid;
    }

}